﻿using Microsoft.AspNetCore.Mvc;
using WebAPI.Entities;
using WebAPI.Services;

namespace WebAPI.Controllers;

[ApiController]
[Route("api/[controller]/[Action]")]
public class TagController : ControllerBase
{
    private TagService _tagService = new TagService();
    
    [HttpPost(Name = "AddTag")]
    public ActionResult AddTag(Tag tag)
    {
        bool flag = _tagService.AddTag(tag);
        if (!flag)
        {
            return BadRequest("创建标签失败，请稍后重试");
        }
        return Ok();
    }
    
    [HttpGet(Name = "GetTags")]
    public ActionResult<Article> GetTags()
    {
        List<Tag> tags = _tagService.GetAllTags();
        if (tags.Count == 0)
        {
            return BadRequest("还未有标签，请稍后重试");
        }
        return Ok(tags);
    }
    
    [HttpPut(Name = "DeleteTag")]
    public ActionResult DeleteTag(Tag tag)
    {
        bool flag = _tagService.DeleteTag(tag);
        if (!flag)
        {
            return BadRequest("删除标签失败，请稍后重试");
        }
        return Ok();
    }
    
    [HttpPut(Name = "UpdateTag")]
    public ActionResult UpdateTag(Tag tag)
    {
        bool flag = _tagService.UpdateTag(tag);
        if (!flag)
        {
            return BadRequest("更新标签失败，请稍后重试");
        }
        return Ok();
    }
}